TOOL.Category = "Weapons"
TOOL.Name = "#Ori Beam Cannon"
TOOL.Command = nil
TOOL.ConfigName = ""
TOOL.Tab = "Stargate"
TOOL.AddToMenu = false -- Tell GMod not to add this STool. aVoN's code will do it later.

local entityName = "ori_beam_cannon"

local models = 
{
   "models/Combine_Scanner.mdl",
   "models/props_c17/furnitureboiler001a.mdl"
}

TOOL.ClientConVar['damage'] = "1000"
TOOL.ClientConVar['model'] = models[1]

cleanup.Register(entityName)

if(CLIENT) then
	language.Add("Tool_"..entityName.."_name", 'Ori Beam Cannon')
	language.Add("Tool_"..entityName.."_desc", 'Creates an Ori beam cannon.')
	language.Add("Tool_"..entityName.."_0", 'Left click to create the beam cannon.')
	language.Add("Undone_"..entityName, 'Beam Cannon Undone')
	language.Add("Cleanup_"..entityName, 'Beam Cannons')
	language.Add("Cleaned_"..entityName, 'Removed all beam cannons')
	language.Add("SBoxLimit_"..entityName, 'Maximum number of beam cannons created')
end

if(SERVER) then 
   CreateConVar("sbox_max"..entityName, 20)
    
	function TOOL:MakeEntity(player, position, upVector, angle)
		if(!player:CheckLimit(entityName)) then 
         return nil 
      end
        
		local entity = ents.Create(entityName)
      entity:SetModel(self:GetClientInfo("model"))
      entity:SetAngles(angle)
      entity:SetPos(position + upVector * entity:BoundingRadius())
      entity:SetVar("Owner", player)
      entity:Setup(self:GetClientNumber("damage"))
      
		entity:Spawn()
        
		player:AddCount(entityName, entity)
		return entity
	end
end

function TOOL:LeftClick(trace)
	if(trace.Entity && trace.Entity:IsPlayer()) then 
      return false 
   elseif(CLIENT) then 
      return true 
   elseif(!self:GetSWEP():CheckLimit(entityName)) then 
      return false 
   end
    
	local player = self:GetOwner()

	local angle = trace.HitNormal:Angle()
   angle.pitch = angle.pitch + 90
   angle.yaw = trace.Normal:Angle().yaw
	
   entity = self:MakeEntity(player, trace.HitPos, trace.HitNormal, angle)
    
	if(trace.HitNonWorld) then
		weld = constraint.Weld(entity, trace.Entity, 0, trace.PhysicsBone, 0, true)
		noCollide = constraint.NoCollide(entity, trace.Entity, 0, trace.PhysicsBone)
		trace.Entity:DeleteOnRemove(entity)
	else
		local physics = entity:GetPhysicsObject()
		
        if(physics:IsValid()) then
			physics:Wake()
			physics:EnableMotion(true)
		end
	end
    
	undo.Create(entityName)
	undo.AddEntity(entity)
    
	if(trace.Entity:IsValid()) then
		undo.AddEntity(weld)
		undo.AddEntity(noCollide)
	end
    
	undo.SetPlayer(player)
	undo.Finish()
	player:AddCleanup(entityName, entity)
    
	return true
end

function TOOL:RightClick(trace)
   if(trace.Entity == nil || trace.Entity:GetClass() ~= entityName) then
      return false
   elseif(CLIENT) then
      return true
   end
   
   local entity = trace.Entity
   
   entity:Setup(self:GetClientNumber("damage"))
   
   return true
end

function TOOL.BuildCPanel(panel)
	panel:AddControl('Header', 
   {
      Text = "#Tool_"..entityName.."_name", 
      Description = "#"..entityName
   })
   
   panel:AddControl("Slider", 
   {
      Label = "Damage:", 
      Type = "Integer", 
      Min = 100, Max = 20000, 
      Command = entityName.."_damage"
   })
   
   for _, model in pairs(models) do
      if(file.Exists("../"..model)) then
         list.Set(entityName.."Models", model, {})
      end
   end
   
   panel:AddControl("PropSelect", 
   {
		Label = "Model:",
		ConVar = entityName.."_model",
		Category = "Stargate",
		Models = list.Get(entityName.."Models")
   })
end
